﻿var ns = namespace("kaskelot.web");

ns.blankett = function () {

	function init() {
		$("#btnSend").click(sendBlankett);
		$("div.errorSummary").hide();
	}

	function sendBlankett() {

		if (!$("#RunwayMasterForm").valid())
			return;

		var prog = kaskelot.web.formutils.getBlankett(".blankett-container");
		var data = "{\"blankett\":" + JSON.stringify(prog) + "}";

		var method = "SendBlankett";

		$.ajax({
			url: window.location.protocol + "//" + window.location.host + "/ClientServices/ClientBlankettService.svc/" + method,
			data: data,
			type: "POST",
			processData: false,
			contentType: "application/json",
			dataType: "json",
			success: function (resp) {
				if (resp["d"].HasError) {
					alert(resp["d"].ErrorMessage);
				}
				else {
					$.blockUI({ message: '<h1>Formulär inskickat!</h1> <p>Klicka OK för att komma till startsidan. </p><input class=\"link-button\" type=\"button\" value=\"Ok\" onclick=\"$.unblockUI(); window.location.href = window.location.protocol + \'//\' + window.location.host;\"/>' });
				}
			},
			error: function (xhr) {
				alert(xhr.responseText);
			}
		});
	}

	return {
		init: init
	}
}();